package com.nengchuan.kbms.dfs.controller;

import com.nengchuan.kbms.dfs.service.feign.WordTransFeginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author luo_zm
 * @DESCRIPTION
 * @create 2020/4/14 16:16
 */
@RestController
@RequestMapping("/api/wordtrans")
@Api(tags = "word文件转换,现在支持word转pdf")
@Slf4j
public class TestWord2PdfController {
    @Autowired
    private WordTransFeginService wordTransFeginService;

    @PostMapping(value = "/file2pdf")
    @ApiOperation(value = "word转pdf传文件")
    public void Word2PdfByFile(@RequestParam("file") MultipartFile file) {
        wordTransFeginService.Word2PdfByFile(file);

        /*String wordDocx = "C:\\Users\\Administrator\\Desktop\\1339257692094496.docx";
        String wordDoc = "C:\\Users\\Administrator\\Desktop\\test.doc";
        String pdf = "C:\\Users\\Administrator\\Desktop\\Test_out_" + System.currentTimeMillis() / 1000 + ".pdf";

        File localFile = new File(wordDocx);
        // 需要导入commons-fileupload的包
        FileItem fileItem = new DiskFileItem("file", Files.probeContentType(localFile.toPath()), false,
                localFile.getName(), (int) localFile.length(), localFile.getParentFile());
        byte[] buffer = new byte[4096];
        int n;
        try (InputStream inputStream = new FileInputStream(localFile); OutputStream os = fileItem.getOutputStream()) {
            while ((n = inputStream.read(buffer, 0, 4096)) != -1) {
                os.write(buffer, 0, n);
            }
            //也可以用IOUtils.copy(inputStream,os);
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @PostMapping(value = "/callback")
    @ApiOperation(value = "回调方法-word转pdf传文件")
    public void CallBack(@RequestParam("fullPath") String fullPath) {
        log.info("文件转档回调返回路径:" + fullPath);
        //TODO 回调逻辑
    }

}
